package com.mysheji.apps.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.mysheji.apps.R;
import com.mysheji.apps.fragment.BbsFragment;
import com.mysheji.apps.fragment.HomeFragment;
import com.mysheji.apps.fragment.MyFragment;
import com.mysheji.apps.utils.ToastUtils;

public class HomeActivity extends AppCompatActivity implements BottomNavigationBar.OnTabSelectedListener  
{
    private BottomNavigationBar bottomNavigationBar;

    private long firstTime;
    private HomeFragment home;
    private BbsFragment bbs;
    private MyFragment my;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        initBottomNavBar();
        initTab();
        bottomNavigationBar.setTabSelectedListener(this);

        //StatusBarUtil.setStatusBarTransparent(this);

        //调用设置//栏颜色
        //StatusBarUtils.setColor(this, getResources().getColor(R.color.colorPrimaryDark));
        //图片置顶
        //StatusBarUtils.setTransparent(this);

    }
    private void initBottomNavBar(){
        /*1.首先进行fvb*/
        bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_nav_bar);
        /*2.进行必要的设置*/
        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);//适应大小
        bottomNavigationBar.setBarBackgroundColor(R.color.bj)//背景色
            .setInActiveColor(R.color.wx)//未选中
            .setActiveColor(R.color.xz);//已选中
        /*3.添加Tab*/
        bottomNavigationBar
            .addItem(new BottomNavigationItem(R.drawable.ic_home_circle,R.string.home))
            .addItem(new BottomNavigationItem( R.drawable.ic_album,R.string.bbs))
            .addItem(new BottomNavigationItem( R.drawable.ic_account_child_circle,R.string.my))
            .setFirstSelectedPosition(0)//默认显示面板
            .initialise();
    }
    private void initTab(){
        home = new HomeFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.frame_content,home);
        transaction.commit();
    }

    @Override
    public void onTabSelected(int position) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        switch (position){
            case 0:
                if(home== null){
                    home= new HomeFragment();
                }
                transaction.replace(R.id.frame_content,home);
                break;
            case 1:
                if(bbs == null){
                    bbs = new BbsFragment();
                }
                transaction.replace(R.id.frame_content,bbs);
                break;
            case 2:
                if(my == null){
                    my = new MyFragment();
                }
                transaction.replace(R.id.frame_content,my);
                break;
        }
        transaction.commit();
    }





    @Override
    public void onTabUnselected(int position) {}
    @Override
    public void onTabReselected(int position) {}



    /*
     //双击退出实现
     @Override 
     public boolean onKeyUp(int keyCode, KeyEvent event)
     { 
     if (keyCode == KeyEvent.KEYCODE_BACK)
     { 
     long secondTime = System.currentTimeMillis(); 
     if (secondTime - firstTime > 2000)
     {//如果两次按键时间间隔大于800毫秒，则不退出 
     ToastUtils.warning(getApplicationContext(), "再按一次退出程序");

     //firstTime = secondTime;//更新firstTime 
     return true; 
     }
     else
     { 
     //System.exit(0);//否则退出程序
     android.os.Process.killProcess(android.os.Process.myPid());  

     } 
     } 
     return super.onKeyUp(keyCode, event); 
     }
     */
    static boolean isExit; 
// 点击两次退出变量
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (!isExit) {
                isExit = true;
                ToastUtils.warning(getApplicationContext(), "再按一次退出程序");
                new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                Thread.sleep(3000);
                                isExit = false;
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }).start();
            } else {
                android.os.Process.killProcess(android.os.Process.myPid());  
                //finish();
            }
            return false;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }
    /*
     补充：

     1.这里的退出程序用的是finish主界面，要真正的杀死程序要这样做：

     android.os.Process.killProcess(android.os.Process.myPid());  


     2.如果你想点击返回键最小化软件，而不是退出程序:

     Intent intent = new Intent();

     intent.setAction("android.intent.action.MAIN");

     intent.addCategory("android.intent.category.HOME");

     MainActivity.this.startActivity(intent);

     来自于代码手册-群：697632515
     */

}

